package com.lenovo.smarttraffic.ui.activity.stage6;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.ParkHistoryBean;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.util.HttpUtils;
import com.lenovo.smarttraffic.util.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class ParkHistoryActivity extends BaseActivity {

    public static List<ParkHistoryBean.ROWSDETAILBean> list = new ArrayList<>();

    private TextView time_tv1, time_tv2;
    private Button find_btn;

    private ListView listView;

    private Calendar ca = Calendar.getInstance();
    private String time1, time2;

    private int flag = 0;

    @Override
    protected int getLayout() {
        return R.layout.activity_park_history;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolBar(this, "停车历史记录");

        time_tv1 = findViewById(R.id.time_tv1);
        time_tv2 = findViewById(R.id.time_tv2);
        find_btn = findViewById(R.id.find_btn);
        listView = findViewById(R.id.listView);

        time_tv1.setOnClickListener(v -> showDatePicker(1));
        time_tv2.setOnClickListener(v -> showDatePicker(2));

        find_btn.setOnClickListener(v -> {
            try {
                if (time1 == null || time2 == null) {
                    ToastUtils.showToast(this, "请设置时间");
                    return;
                }
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
                Date date1 = format.parse(time1);
                Date date2 = format.parse(time2);

                HttpUtils.post("get_car_history", new HttpUtils().add("UserName", MainActivity.userName),
                        response -> {
                            list = new Gson().fromJson(response.toString(), ParkHistoryBean.class).getROWS_DETAIL();
                            List<ParkHistoryBean.ROWSDETAILBean> list2 = new ArrayList<>();

                            try {
                                for (int i = 0; i < list.size(); i++) {
                                    SimpleDateFormat format2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.CHINA);
                                    Date in_time = format2.parse(list.get(i).getCar_in_time());
                                    Date out_time = format2.parse(list.get(i).getCar_out_time());
                                    if (date1.before(in_time) && date2.after(out_time)) {
                                        list2.add(list.get(i));
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            if (list2.size() < 1) {
                                ToastUtils.showToast(this, "未查询到该时间段记录");
                            } else {
                                ToastUtils.showToast(this, "查询到" + list2.size() + "条记录");
                            }
                            listView.setAdapter(new ParkHistoryAdapter(list2));
                        });
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    private void showDatePicker(int flag) {
        this.flag = flag;

        int mYear = ca.get(Calendar.YEAR);
        int mMonth = ca.get(Calendar.MONTH);
        int mDay = ca.get(Calendar.DAY_OF_MONTH);
        int mHour = ca.get(Calendar.HOUR_OF_DAY);
        int mMinute = ca.get(Calendar.MINUTE);

        //Init date picker
        DatePickerDialog dialog = new DatePickerDialog(this, onDateSetListener, mYear, mMonth, mDay);
        dialog.setButton(DatePickerDialog.BUTTON_POSITIVE, getText(android.R.string.ok), (i1, which) -> {
            DatePicker mDatePicker = dialog.getDatePicker();
            // Clearing focus forces the dialog to commit any pending
            // changes, e.g. typed text in a NumberPicker.
            mDatePicker.clearFocus();
            onDateSetListener.onDateSet(mDatePicker, mDatePicker.getYear(),
                    mDatePicker.getMonth(), mDatePicker.getDayOfMonth());

            //Show time picker
            new TimePickerDialog(this, onTimeSetListener, mHour, mMinute, true)
                    .show();
        });
        //Show date picker
        dialog.show();
    }

    private DatePickerDialog.OnDateSetListener onDateSetListener = (view, year, monthOfYear, dayOfMonth) -> {
        String mMonth = (monthOfYear + 1) + "-";
        String mDay = dayOfMonth + " ";

        if (mMonth.length() < 3) {
            mMonth = "0" + mMonth;
        }
        if (mDay.length() < 3) {
            mDay = "0" + mDay;
        }

        String date = year + "-" + mMonth + mDay;
        if (flag == 1) {
            time1 = date;
        } else {
            time2 = date;
        }
    };

    private TimePickerDialog.OnTimeSetListener onTimeSetListener = (view, hourOfDay, minute) -> {
        String mHour = hourOfDay + ":";
        String mMinute = minute + "";

        if (mHour.length() < 3) {
            mHour = "0" + mHour;
        }
        if (mMinute.length() < 2) {
            mMinute = "0" + mMinute;
        }

        String time = mHour + mMinute + ":00";
        if (flag == 1) {
            time1 += time;
            time_tv1.setText("入场时间 " + time1);
        } else {
            time2 += time;
            time_tv2.setText("出场时间 " + time2);
        }
    };

}

class ParkHistoryAdapter extends BaseAdapter {

    private List<ParkHistoryBean.ROWSDETAILBean> data;

    public ParkHistoryAdapter(List<ParkHistoryBean.ROWSDETAILBean> data) {
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_park_history, parent, false);
            holder.id_tv = convertView.findViewById(R.id.id_tv);
            holder.id_tv2 = convertView.findViewById(R.id.id_tv2);
            holder.number_tv = convertView.findViewById(R.id.number_tv);
            holder.time1_tv = convertView.findViewById(R.id.time1_tv);
            holder.time2_tv = convertView.findViewById(R.id.time2_tv);
            holder.money_tv = convertView.findViewById(R.id.money_tv);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ParkHistoryBean.ROWSDETAILBean bean = data.get(position);
        holder.id_tv.setText("" + bean.getId());
        holder.id_tv2.setText(bean.getCar_code());
        holder.number_tv.setText(bean.getCar_number());
        holder.time1_tv.setText(bean.getCar_in_time());
        holder.time2_tv.setText(bean.getCar_out_time());
        holder.money_tv.setText(bean.getCar_consumption_money() + "元");

        return convertView;
    }

    static class ViewHolder {
        TextView id_tv, id_tv2, number_tv, time1_tv, time2_tv, money_tv;
    }
}